--#include "data\functions\Activity\CombineServer\CombineServerRechargeRank.lua" once
--#include "data\functions\Activity\CombineServer\CombineServerConsumeRank.lua" once
--#include "data\functions\Activity\CombineServer\CombineServerGiftBag.lua" once
--#include "data\functions\Activity\CombineServer\CombineServerArena.lua" once					--合服擂台

function GmDoCombineServer( sysarg, flag, param )
	print("GmDoCombineServer, flag="..flag..", param="..param)
	if flag == 1 then
		CombineServerSendData( sysarg )
	elseif flag == 2 then
		CombineServerFreshBoss( param )
	elseif flag == 3 then
		DoCombineServerBuyGiftBag( sysarg, param )
	elseif flag == 4 then
		local sVar = Actor.getStaticVar(sysarg)
		sVar.combineGiftBag = nil		--合服限时礼包领取情况
		CombineServerSendGiftBagData( sysarg )
	elseif flag == 5 then				--合服神秘商店抽取
		CommonRefreshItemFromLib( sysarg, enItemRefreshActivity_CombineShop, param )	--自动/手动刷新
	elseif flag == 6 then
		CommonSendRefreshItemsData( sysarg, enItemRefreshActivity_CombineShop )
	elseif flag == 7 then
		CommonBuyRefreshItem( sysarg, enItemRefreshActivity_CombineShop, param )
	elseif flag == 8 then		--合服擂台报名
		CombineArenaApply( sysarg, param )
	elseif flag == 9 then		--[合服擂台]擂台开始
		CombineArenaBegin( )
	elseif flag == 10 then		--[合服擂台]发擂台、大乱斗奖励
		if param == 1 then
			SendCombineArenaRankAwards( )
		else
			SendCombineArenaFightAwards( )
		end
	elseif flag == 11 then

	elseif flag == 12 then		--[合服擂台]大乱斗开始
		CombineArenaFightBegin( )
	elseif flag == 13 then		--[合服擂台]进入擂台或大乱斗
		if param == 1 then
			CombineArenaEnter( sysarg ) 		--进入擂台
		else
			CombineArenaFightEnter( sysarg )	--进入大乱斗
		end
	end
end

function GetCombineServerRankData(sysarg, packet)
	local opType = DataPack.readByte(packet)
	if opType == 1 then
		CombineServerRechargeRankSendData(sysarg)
	elseif opType == 2 then
		CombineServerConsumeRankSendData(sysarg)
	end
end

--[[
每日0点，重置合服数据，并发送客户端
]]
function CombineServerDailyProc( sysarg )
	local sVar = Actor.getStaticVar(sysarg)
	sVar.combineGiftBag 	= nil		--合服活动的限时礼包领取情况
	sVar.combineSCGiftSts 	= nil		--合服活动的当日最大单笔充值礼包是否领取

	CombineServerSendBossPartyClearStatus()		--清除BOSS状态
	CombineServerSendGiftBagData( sysarg )
	CombineServerSendChargeGiftData( sysarg )
end

--[[
每次登陆
]]
function CombineServerLogin( sysarg )
	if not IsInCombineServerActivity() then
		return
	end

	CombineServerSendGiftBagData( sysarg )
	CombineServerSendChargeGiftData( sysarg )
	CombineServerGiftBagRefresh(sysarg)             --刷新合服礼包判断
end


--[[合服活动刷BOSS
bossId:要刷出的BOSSID，具体配置在CommonBossConfig的
]]
function CombineServerFreshBoss( bossIdx )
	bossIdx = tonumber( bossIdx )
	local freshBoss = CombineServerCfg.bossParty.boss[bossIdx]
	if not freshBoss then
		return
	end

	local combineDay = System.getDaysSinceCombineServer()
	if not IsElemInList( CombineServerCfg.bossParty.day, combineDay ) then
		return
	end

	local freshCfg = GetElemByOpenServerDay( freshBoss )
	if not freshCfg then
		return
	end

	--print("CombineServerFreshBoss, bossId="..bossId)
	local bossCfg = MobBossCommonConfig[freshCfg.bossId]
	if bossCfg then
		freshSceneMonsterInPos( bossCfg )
	end
end

--[[
合服活动，领取礼包
]]
function CombineServerBuyGiftBag( sysarg, packet )
	local giftIdx = DataPack.readByte(packet)
	DoCombineServerBuyGiftBag( sysarg, giftIdx )
end

--[[
合服活动，领取礼包
giftIdx:购买当日第N个特惠礼包，从1-N
]]
function DoCombineServerBuyGiftBag( sysarg, giftIdx )
	local combineDay = System.getDaysSinceCombineServer()
	--print("DoCombineServerBuyGiftBag, combineDay="..combineDay..", giftIdx="..giftIdx)
	local combineDay = System.getDaysSinceCombineServer()
	if not IsElemInList( CombineServerCfg.giftBag.day, combineDay ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer012,ttFlyTip)
		return
	end

	local dayGift = CombineServerCfg.giftBag.gifts[combineDay]
	if not dayGift then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer001,ttFlyTip)
		return
	end

	local gift = dayGift[giftIdx]
	if not gift then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer002,ttFlyTip)
		return
	end

	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.combineGiftBag then
		sVar.combineGiftBag = {}
	end

	local num = sVar.combineGiftBag[giftIdx] or 0
	if num > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer003,ttFlyTip)
		CombineServerSendGiftBagData( sysarg )
		return
	end

	if not IsBagGridEnough(sysarg, gift.awards) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	if gift.consume and not CheckConsumeCond( sysarg, gift.consume, 1, false ) then
		return
	end

	if gift.consume and  not DoConsumeCond( sysarg, gift.consume, 1, GameLog.clCombineServerGiftBagFee, Lang.ScriptTips.CombineServerLog01, false ) then
		return
	end
	sVar.combineGiftBag[giftIdx] = 1
	GiveCommonAward(sysarg, gift.awards, GameLog.clCombineServerGiftBagAdd, Lang.ScriptTips.CombineServerLog02)
	CombineServerSendGiftBagData( sysarg )
end

--[[
发送合服特惠礼包的领取数据
]]
function CombineServerSendGiftBagData( sysarg )
	if not IsInCombineServerActivity() then
		return
	end

	local combineDay = System.getDaysSinceCombineServer()
	local dayGift = CombineServerCfg.giftBag.gifts[combineDay]
	if not dayGift then
		return
	end

	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.combineGiftBag then
		sVar.combineGiftBag = {}
	end

	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCombineServerSendGiftBagData)
	if (pack ~= nil) then
		DataPack.writeChar(pack, #dayGift)
		for giftIdx, gift in ipairs( dayGift ) do
			local num = sVar.combineGiftBag[giftIdx] or 0
			DataPack.writeChar(pack, giftIdx)
			DataPack.writeChar(pack, num)
			--print("CombineServerSendGiftBagData, giftIdx="..giftIdx..", num="..num)
		end
		DataPack.flush(pack)
	end
end

--[[
领取当日充值礼包
]]
function CombineServerGetChargeGift( sysarg )
	if not IsInCombineServerActivity() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
		return false
	end
	local combineDay = System.getDaysSinceCombineServer()
	if not IsElemInList( CombineServerCfg.dailyCharge.day, combineDay ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer013,ttFlyTip)
		return
	end

	--print("CombineServerGetChargeGift, combineDay="..combineDay)
	local chargeGift = CombineServerCfg.dailyCharge.gifts[combineDay]
	if not chargeGift then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer004,ttFlyTip)
		return
	end

	local dailyChargeSingle = Actor.getDailyChargeSingleYuanBao( sysarg )
	--print("CombineServerGetChargeGift, dailyChargeSingle="..dailyChargeSingle..", needChargeSingle="..chargeGift.needChargeSingle)
	if dailyChargeSingle < chargeGift.needChargeSingle then		--检查当日最大单笔充值
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer005,ttFlyTip)
		return
	end

	if not IsBagGridEnough(sysarg, chargeGift.awards) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
		return
	end

	local sVar = Actor.getStaticVar(sysarg)
	local giftSts = sVar.combineSCGiftSts or 0
	if giftSts > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer006, ttFlyTip)
		return
	end

	sVar.combineSCGiftSts = 1
	GiveCommonAward(sysarg, chargeGift.awards, GameLog.clCombineServerChargeGiftAdd, Lang.ScriptTips.CombineServerLog03)
	CombineServerSendChargeGiftData( sysarg )
end

function CombineServerSendChargeGiftData( sysarg )
	if not IsInCombineServerActivity() then
		return
	end

	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCombineServerSendChargeGiftData)
	if (pack ~= nil) then
		local sVar = Actor.getStaticVar(sysarg)
		local giftSts = sVar.combineSCGiftSts or 0
		DataPack.writeChar(pack, giftSts)
		DataPack.flush(pack)

		--print("CombineServerSendChargeGiftData, giftSts="..giftSts)
	end
end

function CombineServerSendData( sysarg )
	CombineServerSendGiftBagData( sysarg )
	CombineServerSendChargeGiftData( sysarg )
	CombineServerSendBossPartyData( sysarg )			--下发BOSS状态
end

--[[
检查是否刷新合服-神秘商店，并扣除消耗
byHand：0-自动刷新，1-手动刷新
]]
function CheckCombineServerMysticalShop( sysarg, byHand )
	--print("CheckCombineServerMysticalShop, byHand="..byHand)
	if not IsInCombineServerActivity() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
		return false
	end

	local combineDay = System.getDaysSinceCombineServer()
	if not IsElemInList( CombineServerCfg.giftBag.day, combineDay ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServer014,ttFlyTip)
		return
	end


	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.mysticalShopFreshTime then
		sVar.mysticalShopFreshTime = 0
	end

	local refreshCd = CombineServerCfg.mysticalShop.refreshCd
	local now = System.getCurrMiniTime()

	--print("--->CheckCombineServerMysticalShop, shopFreshTime="..sVar.mysticalShopFreshTime..", now="..now..", refreshCd="..refreshCd)
	if  now >= sVar.mysticalShopFreshTime + refreshCd then			--CD时间到,免费刷新
		return true
	elseif byHand == 0 then			--自动刷新，且CD时间未到
		return false
	end

	local consumes = CombineServerCfg.mysticalShop.refreshConsume
	--以下扣除消耗，包括免费次数、或道具消耗或元宝等
	if consumes and not CheckConsumeCond( sysarg, consumes, 1, false ) then
		return false
	end

	if consumes and not DoConsumeCond( sysarg, consumes, 1, GameLog.clCombineServerRefreshShopFee, Lang.ScriptTips.CombineServerLog04, false  ) then
		return false
	end

	return true
end

--[[
检查能够购买合服神秘商店物品
]]
function CheckCombineServerMysticalShopBuy( sysarg )
	if not IsInCombineServerActivity() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
		return false
	end

	return true
end


--[[获取神秘商店的商品抽取库
返回：物品库,每次抽取N个物品,是否重复抽取,排行榜Name(nil表示使用角色静态变量存储抽取信息)
]]
function GetCombineServerMysticalShopLib( )
	local mystiaclShopLib = RefreshItemLibCfg[CombineServerCfg.mysticalShop.ItemRefreshLibIdx]
	local drawNum = CombineServerCfg.mysticalShop.drawNum
	return mystiaclShopLib, drawNum, drawSame, nil
end

--[[
返回：刷出物品数据
]]
function GetCombineServerMysticalShopSaveVar( sysarg )
	--print("GetCombineServerMysticalShopSaveVar...")
	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.mysticalShopData then
		sVar.mysticalShopData = {}
	end
	return sVar.mysticalShopData
end

--[[
返回：上次刷新时间、下次自动刷新时间（-1：不自动刷新）
]]
function GetCombineServerMysticalShopTime( sysarg )
	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.mysticalShopFreshTime then
		sVar.mysticalShopFreshTime = 0
	end

	local refreshCd = CombineServerCfg.mysticalShop.refreshCd
	local now = System.getCurrMiniTime()
	local lastFreshTime = (sVar.mysticalShopFreshTime + refreshCd) - now
	if lastFreshTime < 0 then
		lastFreshTime = 0
	end
	--print("GetCombineServerMysticalShopTime, freshTime="..sVar.mysticalShopFreshTime..", now="..now)
	return sVar.mysticalShopFreshTime, lastFreshTime
end

function SetCombineServerMysticalShopTime( sysarg, refreshTime )
	local sVar = Actor.getStaticVar( sysarg )
	sVar.mysticalShopFreshTime = refreshTime
end

--[[
保存神秘商店刷出的物品数据

function SaveCombineServerMysticalShopData( sysarg, items )
	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.mysticalShopData then
		sVar.mysticalShopData = {}
	end

	sVar.mysticalShopFreshTime = System.getCurrMiniTime()
	for idx, item in ipairs( items ) do
		sVar.mysticalShopData[idx] 	= {}
		sVar.mysticalShopData[idx][1]	= item.itemIdx
		sVar.mysticalShopData[idx][2]	= 0						--本人购买数量为0
		sVar.mysticalShopData[idx][3]	= 0						--全服购买数量为0（保留不使用）
		print("SaveCombineServerMysticalShopData, idx="..idx..", itemIdx="..sVar.mysticalShopData[idx][1])
	end
end
]]

--[[
获取神秘商店刷出的物品数据

function GetCombineServerMysticalShopData( sysarg )
	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.mysticalShopData then
		sVar.mysticalShopData = {}
	end

	local refreshTime = sVar.mysticalShopFreshTime or 0
	local items = {}
	local drawNum = CombineServerCfg.mysticalShop.drawNum
	for idx=1, drawNum do
		local itemIdx 	= sVar.mysticalShopData[idx][1] or 0
		local buyNum	= sVar.mysticalShopData[idx][2] or 0
		local allBuyNum	= sVar.mysticalShopData[idx][3] or -1			---1：客户端不处理
		items[idx] = { itemIdx, buyNum, allBuyNum }
	end
	return refreshTime, items
end
]]

function CombineServerBossPartyTeleport( sysarg )
	if not IsInCombineServerActivity() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
		return
	end

	local combineDay = System.getDaysSinceCombineServer()
	local bossIdx 	 = GetCombineServerBossIdxOfParty( combineDay )
	local openDay 	 = System.getDaysSinceOpenServer()
	--print("CombineServerGetChargeGift, combineDay="..combineDay..", openDay="..openDay..", bossIdx="..bossIdx)
	local bossParty = CombineServerCfg.bossParty.boss[bossIdx]
	local bossPartyElem = nil
	for i, elem in ipairs( bossParty ) do
		if elem.openServerDay[1] <= openDay and openDay <= elem.openServerDay[2] then
			bossPartyElem = elem
		end
	end
	if not bossPartyElem then
		return
	end

	CommonTeleportByTeleId( sysarg, bossPartyElem.teleId )
end

function GetCombineServerBossIdxOfParty( combineDay )
	for idx, oneDay in ipairs( CombineServerCfg.bossParty.day ) do
		if oneDay == combineDay then
			return idx
		end
	end
	return 0
end

function CombineServerSendBossPartyData( sysarg )
	if not IsInCombineServerActivity() then
		return
	end

	local combineDay = System.getDaysSinceCombineServer()
	if not IsElemInList( CombineServerCfg.bossParty.day, combineDay ) then
		return
	end

	local bossIdx 	 = GetCombineServerBossIdxOfParty( combineDay )
	local openDay 	 = System.getDaysSinceOpenServer()
	local bossParty = CombineServerCfg.bossParty.boss[bossIdx]
	local bossPartyElem = nil
	for i, elem in ipairs( bossParty ) do
		if elem.openServerDay[1] <= openDay and openDay <= elem.openServerDay[2] then
			bossPartyElem = elem
		end
	end
	if not bossPartyElem then
		return
	end

	local bossSts = GetCommonBossSceneStatus( bossPartyElem.bossId )
	if not IsElemInList( {1,2,3}, bossSts ) then
		return
	end

	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCombineServerSendBossPartyData)
	if (pack ~= nil) then
		DataPack.writeChar(pack, bossSts)
		DataPack.flush(pack)
		--print("CombineServerSendBossPartyData, bossId="..bossPartyElem.bossId..", bossSts="..bossSts)
	end
end

function CombineServerSendBossPartyClearStatus()
	for i, bossParty in ipairs( CombineServerCfg.bossParty.boss ) do
		for j, elem in ipairs( bossParty ) do
			--print("CombineServerSendBossPartyClearStatus, bossId="..elem.bossId)
			SetCommonBossSceneStatus( elem.bossId, 1)		--设置为未刷新
		end
	end
end
